package com.ganmiao.server.sys.service;

import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ganmiao.exception.DefaultBusinessException;
import com.ganmiao.resp.CommonResultRes;
import com.ganmiao.resp.MobBaseRes;
import com.ganmiao.resp.PageResult;
import com.ganmiao.resp.ResponseCode;
import com.ganmiao.server.file.service.GmAttachmentService;
import com.ganmiao.server.supplier.dto.InformationDto;
import com.ganmiao.server.supplier.dto.UpdatePasswordDto;
import com.ganmiao.server.supplier.entity.SupplierUser;
import com.ganmiao.server.supplier.service.SupplierUserService;
import com.ganmiao.server.supplier.vo.InformationVo;
import com.ganmiao.server.sys.dto.SysUserDto;
import com.ganmiao.server.sys.entity.SysUser;
import com.ganmiao.server.sys.entity.SysUserAccount;
import com.ganmiao.server.sys.mapper.SysUserMapper;
import com.ganmiao.server.sys.query.SysUserQuery;
import com.ganmiao.server.sys.repository.SysUserAccountRepository;
import com.ganmiao.server.sys.repository.SysUserRepository;
import com.ganmiao.server.sys.vo.SysUserVo;
import com.ganmiao.server.user.context.UserAuthContext;
import com.ganmiao.server.user.enums.SysCodeEnum;
import com.ganmiao.utils.PageListUtil;
import com.ganmiao.utils.StatusUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 用户信息
 */
@Service
public class SysUserService {

    //用户信息
    @Autowired
    private SysUserRepository sysUserRepository;

    //用户信息
    @Autowired
    private SysUserMapper sysUserMapper;

    //用户账号
    @Autowired
    private SysUserAccountService sysUserAccountService;

    //用户角色
    @Autowired
    private SysUserRoleService sysUserRoleService;

    //附件信息
    @Autowired
    private GmAttachmentService gmAttachmentService;

    @Autowired
    private SupplierUserService supplierUserService;

    @Autowired
    private SysUserAccountRepository sysUserAccountRepository;

//    @Autowired
//    private ShopUserService shopUserService;

    /**
     * 新增用户信息
     *
     * @param sysUserDto
     * @return
     */
    @Transactional
    public MobBaseRes addSysUser(SysUserDto sysUserDto) {

        //获取系统编码
        String sysCode = UserAuthContext.getSysCode();
        SysUserVo loginUser = UserAuthContext.getLoginUser();

        //新增用户信息
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUserDto, sysUser);
        sysUser.setSysCode(sysCode);
        boolean save = sysUserRepository.save(sysUser);

        if (StringUtils.isNotBlank(sysUserDto.getUserAccount()) && StringUtils.isNotBlank(sysUserDto.getUserPassword())) {
            //用户账号
            SysUserAccount sysUserAccount = SysUserAccount.builder()
                    .userId(sysUser.getUserId())
                    .userAccount(sysUserDto.getUserAccount())
                    .userPassword(DigestUtil.md5Hex(sysUserDto.getUserPassword()))
                    .sysCode(sysCode)
                    .build();
            //新增用户账号信息
            sysUserAccountService.addSysUserAccount(sysUserAccount);
        }

        if (sysCode.equals(SysCodeEnum.SUPPLIER.getCode())) {
            SupplierUser supplierUser = SupplierUser.builder()
                    .userId(sysUser.getUserId())
                    .supplierId(loginUser.getSupplierId())
                    .build();
            supplierUserService.add(supplierUser);
        }

//        if (sysCode.equals(SysCodeEnum.SUPERMARKET.getCode())) {
//            ShopUser shopUser = ShopUser.builder()
//                    .userId(sysUser.getUserId())
//                    .shopId(loginUser.getShopId())
//                    .build();
//            shopUserService.add(shopUser);
//        }

        //新增用户角色信息
        sysUserRoleService.addSysUserRoleByUserId(sysUser.getUserId(), sysUserDto.getRoleIds());

        return save ? new MobBaseRes(ResponseCode.SUCCESS.getCode(), "新增成功") : new MobBaseRes(ResponseCode.FAIL.getCode(), "新增失败");

    }

    /**
     * 新增用户信息
     *
     * @param sysUserDto
     * @return
     */
    @Transactional
    public MobBaseRes addSupplierUser(SysUserDto sysUserDto) {

        //获取系统编码
        String sysCode = UserAuthContext.getSysCode();
        SysUserVo loginUser = UserAuthContext.getLoginUser();

        //新增用户信息
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUserDto, sysUser);
        sysUser.setSysCode(sysCode);
        boolean save = sysUserRepository.save(sysUser);

        if (StringUtils.isNotBlank(sysUserDto.getUserAccount()) && StringUtils.isNotBlank(sysUserDto.getUserPassword())) {
            //用户账号
            SysUserAccount sysUserAccount = SysUserAccount.builder()
                    .userId(sysUser.getUserId())
                    .userAccount(sysUserDto.getUserAccount())
                    .userPassword(DigestUtil.md5Hex(sysUserDto.getUserPassword()))
                    .sysCode(sysCode)
                    .build();
            //新增用户账号信息
            sysUserAccountService.addSysUserAccount(sysUserAccount);
        }

        if (sysCode.equals(SysCodeEnum.SUPPLIER.getCode())) {
            SupplierUser supplierUser = SupplierUser.builder()
                    .userId(sysUser.getUserId())
                    .supplierId(loginUser.getSupplierId())
                    .build();
            supplierUserService.add(supplierUser);
        }

        //新增用户角色信息
        sysUserRoleService.addSysUserRoleByUserId(sysUser.getUserId(), sysUserDto.getRoleIds());

        return save ? new MobBaseRes(ResponseCode.SUCCESS.getCode(), "新增成功") : new MobBaseRes(ResponseCode.FAIL.getCode(), "新增失败");

    }

//    /**
//     * 商超用户信息新增
//     *
//     * @param sysUserDto
//     * @return
//     */
//    @Transactional
//    public MobBaseRes addShopUser(SysUserDto sysUserDto) {
//
//        //获取系统编码
//        String sysCode = UserAuthContext.getSysCode();
//        SysUserVo loginUser = UserAuthContext.getLoginUser();
//
//        if (Objects.isNull(loginUser)) {
//            throw new DefaultBusinessException("商超用户登录信息错误");
//        }
//
//        //新增用户信息
//        SysUser sysUser = new SysUser();
//        BeanUtils.copyProperties(sysUserDto, sysUser);
//        sysUser.setSysCode(sysCode);
//        boolean save = sysUserRepository.save(sysUser);
//
//        if (StringUtils.isNotBlank(sysUserDto.getUserAccount()) && StringUtils.isNotBlank(sysUserDto.getUserPassword())) {
//            //用户账号
//            SysUserAccount sysUserAccount = SysUserAccount.builder()
//                    .userId(sysUser.getUserId())
//                    .userAccount(sysUserDto.getUserAccount())
//                    .userPassword(DigestUtil.md5Hex(sysUserDto.getUserPassword()))
//                    .sysCode(sysCode)
//                    .build();
//            //新增用户账号信息
//            sysUserAccountService.addSysUserAccount(sysUserAccount);
//        }
//
//        //新增用户角色信息
//        sysUserRoleService.addSysUserRoleByUserId(sysUser.getUserId(), sysUserDto.getRoleIds());
//
//        if (sysUserDto.getShopIds().isEmpty()) {
//            throw new DefaultBusinessException("商超ID集合不能为空");
//        }
//        //新增商超用户信息
//        shopUserService.addShopUser(sysUserDto.getShopIds(), sysUser.getUserId());
//
//        return save ? new MobBaseRes(ResponseCode.SUCCESS.getCode(), "新增成功") : new MobBaseRes(ResponseCode.FAIL.getCode(), "新增失败");
//
//    }

    /**
     * 删除用户信息
     *
     * @param userId
     * @return
     */
    @Transactional
    public MobBaseRes deleteSysUser(Integer userId) {

        //删除条件
        LambdaUpdateWrapper<SysUser> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SysUser::getUserId, userId)
                .set(SysUser::getStatus, StatusUtils.FAILURE);
        //删除结果
        boolean update = sysUserRepository.update(updateWrapper);

        //删除用户账号信息
        sysUserAccountService.deleteSysUserAccount(userId);

        //删除用户角色信息
        sysUserRoleService.deleteSysUserRoleByUserId(userId);

        supplierUserService.delete(userId);

        return update ? new MobBaseRes(ResponseCode.SUCCESS.getCode(), "删除成功") : new MobBaseRes(ResponseCode.FAIL.getCode(), "删除失败");

    }

//    /**
//     * 商超用户信息删除
//     *
//     * @param userId
//     * @return
//     */
//    @Transactional
//    public MobBaseRes deleteShopSysUser(Integer userId) {
//
//        //删除条件
//        LambdaUpdateWrapper<SysUser> updateWrapper = new LambdaUpdateWrapper<>();
//        updateWrapper.eq(SysUser::getUserId, userId)
//                .set(SysUser::getStatus, StatusUtils.FAILURE);
//        //删除结果
//        boolean update = sysUserRepository.update(updateWrapper);
//
//        //删除用户账号信息
//        sysUserAccountService.deleteSysUserAccount(userId);
//
//        //删除用户角色信息
//        sysUserRoleService.deleteSysUserRoleByUserId(userId);
//
//        shopUserService.deleteShopSysUser(userId);
//
//        return update ? new MobBaseRes(ResponseCode.SUCCESS.getCode(), "删除成功") : new MobBaseRes(ResponseCode.FAIL.getCode(), "删除失败");
//
//    }

    /**
     * 修改用户信息
     *
     * @param sysUserDto
     * @return
     */
    @Transactional
    public MobBaseRes updateSysUser(SysUserDto sysUserDto) {

        //修改用户信息
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUserDto, sysUser);
        boolean update = sysUserRepository.updateById(sysUser);

        //用户账号
        SysUserAccount sysUserAccount = SysUserAccount.builder()
                .userId(sysUserDto.getUserId())
                .userAccount(sysUserDto.getUserAccount())
                .sysCode(sysUserDto.getSysCode())
                .build();
        if (!sysUserDto.getUserPassword().equals("******")) {
            sysUserAccount.setUserPassword(DigestUtil.md5Hex(sysUserDto.getUserPassword()));
        }
        //修改用户账号信息
        boolean account = sysUserAccountService.updateSysUserAccount(sysUserAccount);

        //修改用户角色信息
        sysUserRoleService.updateSysUserRoleByUserId(sysUserDto.getUserId(), sysUserDto.getRoleIds());

        return update && account ? new MobBaseRes(ResponseCode.SUCCESS.getCode(), "修改成功") : new MobBaseRes(ResponseCode.FAIL.getCode(), "修改失败");

    }

    /**
     * 修改用户信息
     *
     * @param sysUserDto
     * @return
     */
    @Transactional
    public MobBaseRes updateSupplierUser(SysUserDto sysUserDto) {

        //修改用户信息
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUserDto, sysUser);
        boolean update = sysUserRepository.updateById(sysUser);

        //用户账号
        SysUserAccount sysUserAccount = SysUserAccount.builder()
                .userId(sysUserDto.getUserId())
                .userAccount(sysUserDto.getUserAccount())
                .sysCode(sysUserDto.getSysCode())
                .build();
        if (!sysUserDto.getUserPassword().equals("******")) {
            sysUserAccount.setUserPassword(DigestUtil.md5Hex(sysUserDto.getUserPassword()));
        }
        //修改用户账号信息
        boolean account = sysUserAccountService.updateSysUserAccount(sysUserAccount);

        //修改用户角色信息
        sysUserRoleService.updateSysUserRoleByUserId(sysUserDto.getUserId(), sysUserDto.getRoleIds());

        return update && account ? new MobBaseRes(ResponseCode.SUCCESS.getCode(), "修改成功") : new MobBaseRes(ResponseCode.FAIL.getCode(), "修改失败");

    }

//    /**
//     * 商超用户信息修改
//     *
//     * @param sysUserDto
//     * @return
//     */
//    @Transactional
//    public MobBaseRes updateShopSysUser(SysUserDto sysUserDto) {
//
//        //修改用户信息
//        SysUser sysUser = new SysUser();
//        BeanUtils.copyProperties(sysUserDto, sysUser);
//        boolean update = sysUserRepository.updateById(sysUser);
//
//        //用户账号
//        SysUserAccount sysUserAccount = SysUserAccount.builder()
//                .userId(sysUserDto.getUserId())
//                .userAccount(sysUserDto.getUserAccount())
//                .sysCode(sysUserDto.getSysCode())
//                .build();
//        if (!sysUserDto.getUserPassword().equals("******")) {
//            sysUserAccount.setUserPassword(DigestUtil.md5Hex(sysUserDto.getUserPassword()));
//        }
//        //修改用户账号信息
//        boolean account = sysUserAccountService.updateSysUserAccount(sysUserAccount);
//
//        //修改用户角色信息
//        sysUserRoleService.updateSysUserRoleByUserId(sysUserDto.getUserId(), sysUserDto.getRoleIds());
//
//        //修改商超用户信息
//        shopUserService.updateShopUser(sysUserDto.getShopIds(), sysUser.getUserId());
//
//        return update && account ? new MobBaseRes(ResponseCode.SUCCESS.getCode(), "修改成功") : new MobBaseRes(ResponseCode.FAIL.getCode(), "修改失败");
//
//    }

    /**
     * 获取用户信息(分页)
     *
     * @param sysUserQuery
     * @return
     */
    public PageResult<SysUserVo> getSysUserList(SysUserQuery sysUserQuery) {

        //获取系统编码
        String sysCode = UserAuthContext.getSysCode();
        sysUserQuery.setSysCode(sysCode);

        //获取用户信息
        SysUserVo loginUser = UserAuthContext.getLoginUser();
        if (loginUser != null && loginUser.getSupplierId() != null) {
            sysUserQuery.setSupplierId(loginUser.getSupplierId());
        }

        if (loginUser != null && loginUser.getShopId() != null) {
            sysUserQuery.setShopId(loginUser.getShopId());
        }

        //分页
        IPage iPage = new Page(sysUserQuery.getPage(), sysUserQuery.getPageSize());

        //查询结果
        IPage<SysUserVo> sysUserList = sysUserMapper.getSysUserList(iPage, sysUserQuery);

        return PageListUtil.getPageResult(sysUserList);

    }

    /**
     * 获取用户信息(分页)
     *
     * @param sysUserQuery
     * @return
     */
    public PageResult<SysUserVo> getSupplierSysUserList(SysUserQuery sysUserQuery) {

        //获取系统编码
        String sysCode = UserAuthContext.getSysCode();
        sysUserQuery.setSysCode(sysCode);

        //获取用户信息
        SysUserVo loginUser = UserAuthContext.getLoginUser();
        if (loginUser != null && loginUser.getSupplierId() != null) {
            sysUserQuery.setSupplierId(loginUser.getSupplierId());
        }

        if (loginUser != null && loginUser.getShopId() != null) {
            sysUserQuery.setShopId(loginUser.getShopId());
        }

        //分页
        IPage iPage = new Page(sysUserQuery.getPage(), sysUserQuery.getPageSize());

        //查询结果
        IPage<SysUserVo> sysUserList = sysUserMapper.getSysUserList(iPage, sysUserQuery);

        return PageListUtil.getPageResult(sysUserList);

    }

//    /**
//     * 商超用户分页列表
//     *
//     * @param sysUserQuery
//     * @return
//     */
//    public PageResult<SysUserVo> shopSysUserList(SysUserQuery sysUserQuery) {
//
//        //获取系统编码
//        String sysCode = UserAuthContext.getSysCode();
//        sysUserQuery.setSysCode(sysCode);
//
//        //获取用户信息
//        SysUserVo loginUser = UserAuthContext.getLoginUser();
//        if (Objects.isNull(loginUser)) {
//            throw new DefaultBusinessException("商超用户信息错误");
//        }
//        sysUserQuery.setShopId(loginUser.getShopId());
//        //分页
//        IPage iPage = new Page(sysUserQuery.getPage(), sysUserQuery.getPageSize());
//
//        //查询结果
//        IPage<SysUserVo> sysUserList = sysUserMapper.shopSysUserList(iPage, sysUserQuery);
//        List<SysUserVo> records = sysUserList.getRecords();
//        for (SysUserVo record : records) {
//            List<Integer> shopIds = shopUserService.getShopIds(record.getUserId());
//            record.setShopIds(shopIds);
//        }
//
//        return PageListUtil.getPageResult(sysUserList);
//
//    }

    /**
     * 获取用户信息(详情)
     *
     * @param userId
     * @return
     */
    public SysUserVo getSysUserById(Integer userId) {

        //查询结果
        SysUserVo sysUserById = sysUserMapper.getSysUserById(userId);
        sysUserById.setUserPassword("******");
        sysUserById.setRoleIds(sysUserRoleService.getSysUserRoleByUserId(userId));
        if (sysUserById.getHeadPicId() != null) {
            sysUserById.setHeadPic(gmAttachmentService.getByFileId(sysUserById.getHeadPicId()).getFileUrl());
        }

        return sysUserById;
    }

    /**
     * 获取用户信息(详情)
     *
     * @param userId
     * @return
     */
    public SysUserVo getSupplierSysUserById(Integer userId) {

        //查询结果
        SysUserVo sysUserById = sysUserMapper.getSysUserById(userId);
        sysUserById.setUserPassword("******");
        sysUserById.setRoleIds(sysUserRoleService.getSysUserRoleByUserId(userId));
        if (sysUserById.getHeadPicId() != null) {
            sysUserById.setHeadPic(gmAttachmentService.getByFileId(sysUserById.getHeadPicId()).getFileUrl());
        }

        return sysUserById;
    }

//    /**
//     * 商超用户详情
//     *
//     * @param userId
//     * @return
//     */
//    public SysUserVo shopSysUserById(Integer userId) {
//
//        //查询结果
//        SysUserVo sysUserById = sysUserMapper.getSysUserById(userId);
//        sysUserById.setUserPassword("******");
//        sysUserById.setRoleIds(sysUserRoleService.getSysUserRoleByUserId(userId));
//        if (sysUserById.getHeadPicId() != null) {
//            sysUserById.setHeadPic(gmAttachmentService.getByFileId(sysUserById.getHeadPicId()).getFileUrl());
//        }
//        List<Integer> shopIds = shopUserService.getShopIds(userId);
//        sysUserById.setShopIds(shopIds);
//        return sysUserById;
//    }

    /**
     * 新增供应商用户信息
     *
     * @param sysUserDto
     * @return
     */
    @Transactional
    public Integer addSupplierSysUser(SysUserDto sysUserDto) {

        //新增用户信息
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUserDto, sysUser);
        sysUserRepository.save(sysUser);

        if (StringUtils.isNotBlank(sysUserDto.getUserAccount()) && StringUtils.isNotBlank(sysUserDto.getUserPassword())) {
            //用户账号
            SysUserAccount sysUserAccount = SysUserAccount.builder()
                    .userId(sysUser.getUserId())
                    .userAccount(sysUserDto.getUserAccount())
                    .userPassword(DigestUtil.md5Hex(sysUserDto.getUserPassword()))
                    .sysCode(sysUserDto.getSysCode())
                    .build();
            //新增用户账号信息
            sysUserAccountService.addSysUserAccount(sysUserAccount);
        }

        return sysUser.getUserId();

    }

    /**
     * 新增商超用户信息
     *
     * @param sysUserDto
     * @return
     */
    @Transactional
    public Integer addShopSysUser(SysUserDto sysUserDto) {

        //新增用户信息
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUserDto, sysUser);
        sysUserRepository.save(sysUser);

        if (StringUtils.isNotBlank(sysUserDto.getUserAccount()) && StringUtils.isNotBlank(sysUserDto.getUserPassword())) {
            //用户账号
            SysUserAccount sysUserAccount = SysUserAccount.builder()
                    .userId(sysUser.getUserId())
                    .userAccount(sysUserDto.getUserAccount())
                    .userPassword(DigestUtil.md5Hex(sysUserDto.getUserPassword()))
                    .sysCode(sysUserDto.getSysCode())
                    .status("9")
                    .build();
            //新增用户账号信息
            sysUserAccountService.addSysUserAccount(sysUserAccount);
        }

        return sysUser.getUserId();

    }

    /**
     * 用户状态变更
     *
     * @return
     */
    public boolean change(Integer userId, String status) {
        sysUserAccountService.change(userId, status);
        LambdaUpdateWrapper<SysUser> updateWrapper = new LambdaUpdateWrapper();
        updateWrapper.eq(SysUser::getUserId, userId)
                .set(SysUser::getStatus, status);
        return sysUserRepository.update(updateWrapper);
    }


    /**
     * 查询个人信息
     *
     * @return
     */
    public CommonResultRes<InformationVo> getInformation() {
        SysUserVo loginUser = UserAuthContext.getLoginUser();
        if (Objects.isNull(loginUser)) {
            throw new DefaultBusinessException(ResponseCode.TOKEN_EXPIRED_EXCEPTION);
        }
        //查询结果
        InformationVo supplierInformationVo = sysUserMapper.getInformationById(loginUser.getUserId());
        if (supplierInformationVo.getHeadPicId() != null) {
            //通过头像id设置头像路径
            supplierInformationVo.setHeadPic(gmAttachmentService.getByFileId(supplierInformationVo.getHeadPicId()).getFileUrl());
        }

        return new CommonResultRes<>(supplierInformationVo);
    }

    /**
     * 修改个人信息
     *
     * @param
     * @return
     */
    public MobBaseRes updateInformation(InformationDto informationDto) {
        SysUserVo loginUser = UserAuthContext.getLoginUser();
        if (Objects.isNull(loginUser)) {
            throw new DefaultBusinessException(ResponseCode.TOKEN_EXPIRED_EXCEPTION);
        }

        SysUser sysUser = new SysUser();
        sysUser.setUserId(loginUser.getUserId());
        BeanUtils.copyProperties(informationDto, sysUser);
        boolean update = sysUserRepository.updateById(sysUser);
        //用户账号
        SysUserAccount sysUserAccount = SysUserAccount.builder()
                .userId(informationDto.getUserId())
                .userAccount(informationDto.getUserAccount())
                .sysCode(informationDto.getSysCode())
                .build();
        //修改用户账号信息
        boolean account = sysUserAccountService.updateSysUserAccount(sysUserAccount);

        return update && account ? new MobBaseRes(ResponseCode.SUCCESS.getCode(), "修改成功") : new MobBaseRes(ResponseCode.FAIL.getCode(), "修改失败");

    }

    public MobBaseRes updatePassword(UpdatePasswordDto updatePasswordDto) {
        SysUserVo loginUser = UserAuthContext.getLoginUser();
        if (Objects.isNull(loginUser)) {
            throw new DefaultBusinessException(ResponseCode.TOKEN_EXPIRED_EXCEPTION);
        }
        if (!updatePasswordDto.getPassword().equals(updatePasswordDto.getConfirmPassword())) {
            throw new DefaultBusinessException("两次输入密码不一致");
        }
        LambdaQueryWrapper<SysUserAccount> sysUserAccountLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysUserAccountLambdaQueryWrapper.eq(SysUserAccount::getStatus, StatusUtils.PASS);
        sysUserAccountLambdaQueryWrapper.eq(SysUserAccount::getUserId, loginUser.getUserId());
        SysUserAccount one = sysUserAccountRepository.getOne(sysUserAccountLambdaQueryWrapper);
        if (one == null) {
            throw new DefaultBusinessException("该账号不存在");
        }
        String md5Hex = DigestUtil.md5Hex(updatePasswordDto.getPassword());
        one.setUserPassword(md5Hex);
        return sysUserAccountRepository.updateById(one) ?
                new MobBaseRes(ResponseCode.SUCCESS.getCode(), "修改成功") :
                new MobBaseRes(ResponseCode.FAIL.getCode(), "修改失败");
    }

    /**
     * 商超修改密码
     *
     * @param password
     * @return
     */
    public MobBaseRes updatePassword(String password) {
        SysUserVo loginUser = UserAuthContext.getLoginUser();
        if (Objects.isNull(loginUser)) {
            throw new DefaultBusinessException(ResponseCode.TOKEN_EXPIRED_EXCEPTION);
        }
        LambdaQueryWrapper<SysUserAccount> sysUserAccountLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysUserAccountLambdaQueryWrapper.eq(SysUserAccount::getStatus, StatusUtils.PASS);
        sysUserAccountLambdaQueryWrapper.eq(SysUserAccount::getUserId, loginUser.getUserId());
        SysUserAccount one = sysUserAccountRepository.getOne(sysUserAccountLambdaQueryWrapper);
        if (one == null) {
            throw new DefaultBusinessException("该账号不存在");
        }
        String md5Hex = DigestUtil.md5Hex(password);
        one.setUserPassword(md5Hex);
        return sysUserAccountRepository.updateById(one) ?
                new MobBaseRes(ResponseCode.SUCCESS.getCode(), "修改成功") :
                new MobBaseRes(ResponseCode.FAIL.getCode(), "修改失败");
    }

}
